<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php 
include('menu.php'); 
include("../connect/connect.php");
?>
<script language="javascript">
function ListSub(SelectValue)
{
	var result;
	var result2;
	<?php 
	$cQuery = mysql_query("SELECT category_id FROM category");
	while($cRow = mysql_fetch_array($cQuery)){
	?>
	if(SelectValue==<?php echo $cRow['category_id'];?>){
		<?php 
		$vQuery = mysql_query("SELECT category_head FROM category WHERE category_id = '".$cRow['category_id']."'");
		$vRow = mysql_fetch_array($vQuery);
		
		$pQuery = mysql_query("SELECT max(code) as code FROM product WHERE category_id = '".$cRow['category_id']."'");
		$pRow = mysql_fetch_array($pQuery);
		$code = $pRow['code']+1;
		$codeShow = sprintf("%04d",$code);
		?>
		result = "<?php echo $vRow['category_head'].$codeShow;?>";
		result2 = "<?php echo $codeShow;?>";
	}
	<?php } ?>

	if(SelectValue==""){
		result = "<font color=\"red\">รหัสสร้างอัตโนมัติหลังจากเลือกหมวดสินค้า</font>";
		result2 = "";
	}
	
	document.getElementById("lacode").innerHTML = result;
	document.getElementById("code").value = result2;
}

function open_popup(){
	var name = document.getElementById('name');
	var category_id = document.getElementById('category_id');
	var cost = document.getElementById('cost');
	var price = document.getElementById('price');
	var rop = document.getElementById('rop');

	if(name.value == ""){
		alert('กรุณากรอกชื่อสินค้า');
		name.focus();
		return false;
	}

	if(cost.value == ""){
		alert('กรุณากรอกต้นทุน');
		cost.focus();
		return false;
	}

	if(price.value == ""){
		alert('กรุณากรอกราคาขาย');
		price.focus();
		return false;
	}
	
	if(rop.value == ""){
		alert('กรุณากรอกจุดสั่งซื้อ');
		rop.focus();
		return false;
	}

	document.formedit.submit();
}
</script>
</head>
<body>
<?php

//แก้ไข
if($_GET["action"]=="update") {

	if($_POST['name'] != "" AND $_POST['cost'] != "" AND $_POST['price'] != "" AND $_POST['rop'] != "")
		{
			$Query = "UPDATE product SET name = '".$_POST["name"]."',cost = '".$_POST["cost"]."',price = '".$_POST["price"]."',rop = '".$_POST["rop"]."' WHERE product_id = '".$_POST["product_id"]."' ";
			$objQuery = mysql_query($Query);
			
			echo "<script type=\"text/javascript\"> window.location= 'product_manage.php';</script>";
			exit();
		}
}

$Query = "SELECT * FROM product,category WHERE product.category_id = category.category_id and product_id = '".$_GET["id"]."'";
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
$row = mysql_fetch_array($objQuery);
?>
<form action="?action=update" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>แก้ไขข้อมูลสินค้า</h1>
    <div class="buttons"><a onclick="open_popup();" class="button"><span>บันทึก</span></a><a onclick="window.history.go(-1);" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
      <tr>
        <td>รหัสสินค้า : </td>
        <td><label id="lacode"><?php echo $row["code"];?></label>
		  <input name="product_id" type="hidden" id="product_id" value="<?php echo $row["product_id"];?>" /></td>
      </tr>
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><input name="name" type="text" id="name" value="<?php echo $row["name"];?>" onKeyUp="IsScharacter(this.value,this)" /></td>
      </tr>
      <tr>
        <td>หมวดสินค้า : </td>
        <td><?php echo $row["category_name"];?></td>
      </tr>
      <tr>
        <td>ต้นทุน : </td>
        <td><input name="cost" type="text" id="cost" value="<?php echo $row["cost"];?>" onKeyUp="IsNumeric(this.value,this)" /></td>
      </tr>
      <tr>
        <td>ราคาขาย : </td>
        <td><input name="price" type="text" id="price" value="<?php echo $row["price"];?>" onKeyUp="IsNumeric(this.value,this)" /></td>
      </tr>
      <tr>
        <td>จุดสั่งซื้อ : </td>
        <td><input name="rop" type="text" id="rop" value="<?php echo $row["rop"];?>" onKeyUp="IsNumeric(this.value,this)" /></td>
      </tr>
    </table>
  </div>
</div>
</form>
</body>
<?php mysql_close(); ?>
</html>
